        TITL DAI FIRMWARE 2E1FD-2E315  V1.1
        ORG   :E1FD
*
*
*
************************************
* INITIALISE SCREEN CHARACTER AREA *
************************************
*
* Fills screen with spaces (clears screen).
* The line mode byte is set #7A, the line colour
* byte to #40, all colour bytes to #00 (4-colour
* text), all character bytes to #20.
*
* Entry: HL: 1st byte after header.
*        DE: end character area.
* Exit:  All registers preserved.
*
FILLS   PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
FIS10   MVI   M,:7A     Set control byte for char
 REM mode
        DCX   H
        MVI   M,:40     Set line colour byte
        DCX   H
FIS20   MVI   B,:42     Number of bytes/line
FIS30   MVI   M,:20     Data byte is space
        DCX   H
        MVI   M,:00     Colour byte is 00
        DCX   H
        DCR   B
        JNZ   FIS30     Next screen addr
        CALL  COMP      All lines done ?
        JNZ   FIS10     Next line if not
        JMP   XRET      Popall, ret
*
****************************
* CHANGE TO CHARACTER MODE *
****************************
*
* If a character is output when the screen is in
* all-graphic mode, the mode is changed to the
* corresponding split-mode.
* If not sufficient space available, mode 0 is
* tried. If still insufficient space, the emergency
* stop routine is used.
*
TMODE   PUSH  PSW
        LDA   COL1      Get current screen mode
        RRC             Already character mode ?
        JC    TMD10     Abort if true
        STC             CY=1
        RAL             Set for split mode
        CALL  SSETM     Change mode
        MVI   A,:FF
        CC    SSETM     Change to mode 0 if not
 REM sufficient space
        JC    TMD20     Emergency stop if still
 REM insufficient space
TMD10   POP   PSW
        RET 
 REM
* If no space for A-mode or mode 0:
 REM
TMD20   LHLD  AESTOP    Get addr emergency stop
 REM routine
        PCHL            Go to this routine
*
********************
* SET TEXT COLOURS *
********************
*
* The COLORT parameters are set: the header
* and trailer of the character area are
* initialised.
* The colour values are between 0 and F. The
* top 4 bits are ignored.
* The colour change is immediate.
*
* The 1st 2 colours are the default background
* and foreground colours for characters. The last
* 2 are alternative, and may be used for (e.g.)
* the cursor. Colours may be repeated.
*
* Entry: HL points to a vector of 4 bytes containing
*        the colours to be set.
* Exit:  All registers preserved.
*
SCOLT   PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        LXI   D,ERRLIN  Addr 1st byte colour
 REM register memory
        CALL  VCOPY     Init. COLORT reg memory
        LDA   COL1      Get current screen mode
        RAR             Char mode?
        LHLD  CHS       Get startaddr char area
        CC    BCOLS     If char mode: set colours
 REM header area
        LHLD  SCE       Get addr end of screen
        CC    BCOLS     If char mode: set colours
 REM trailer area
        JMP   XRET      Popall, ret
*
*************************
* SET COLOUR PARAMETERS *
*************************
*
* Loads colour data from ROM into the RAM pointers.
* The high nibbles are 8x, 9x, Ax, Bx.
* Used for both COLORT and COLORG.
*
* Entry: HL: Points to colour parameters.
*        DE: Address colour memory in RAM.
* Exit:  DE: Points after colour memory.
*        Other registers corrupted.
*
VCOPY   LXI   B,:1080
VCP10   MOV   A,M       Get colour from ROM
        ANI   :0F
        ORA   C         Add bits 4-7
        STAX  D         Store in RAM
        INX   H         Next colour
        INX   D         Next RAM location
        MOV   A,C       )
        ADD   B         ) Add #10 to C
        MOV   C,A       )
        CPI   :C0       Check if finished
        JNZ   VCP10     Next one if not
        RET 
*
***************************************
* LOAD COLOURS IN HEADER/TRAILER AREA *
***************************************
*
* Sets blanking area colour bytes according to
* information given.
* The colourbytes for the character area are
* loaded into the screen header and trailer area.
*
* Entry: HL: Points to 1st control byte after
*            blanking area.
*        DE: Points after table with colours
*            in RAM.
* Exit:  AFDE preserved, BCHL corrupted.
*
BCOLS   PUSH  PSW
        PUSH  D
        LXI   B,:0004   Distance between colour byte
        DCX   H         Addr 1st colour byte of
 REM screen RAM
BCS10   DCX   D         Addr colour table
        LDAX  D         Get colour byte
        DAD   B         HL = addr in screen RAM
        MOV   M,A       Load byte into screen RAM
        ANI   :30       Finished ?
        JNZ   BCS10     Next colourbyte if not
        POP   D
        POP   PSW
        RET 
*
***********************
* SET CURSOR POSITION *
***********************
*
* Moves the cursor from its current position to
* any requested position.
* Position 0,0 is the bottom left corner.
*
* Entry: HL contains the y,x position required
*           for the cursor.
* Exit:  BCDEHL preserved.
*        CY=0: OK. F corrupted, A preserved.
*        CY=1: Request off screen.
*              A=01 (error code 'off screen').
*
SCURS   ORA   A
        PUSH  H
        PUSH  D
        PUSH  B
        PUSH  PSW
        MOV   A,L       X-coord in A
        CPI   :3C       After end of line ?
        JNC   SCS30     Then request off screen
        ADD   A         X-coord *2
        MOV   C,A       in C
        MVI   B,:18     Nr of lines in mode 0
        LDA   COL1      Get current screen mode
        ORA   A
        JM    SCS10     Jump if mode 0
        MVI   B,:04     Nr of lines in A-modes
        RAR 
        JNC   SCS30     Error if all-graphics mode
SCS10   MOV   A,H       Y-coord in A
        CMP   B         More than max value
        JNC   SCS30     Then request off screen
        CALL  CURDEL    Delete old cursor
        INR   A
        LXI   H,:0086   Length 1 char line
        CALL  HLMUL     Calc length reqd number of
 REM lines (HL=A*HL)
        XCHG            in DE
        LHLD  GAE       Store end archive area
        DAD   D         Start of reqd line
        CALL  SSETL     Store addr line mode byte
 REM current line and store last
 REM byte on that line
        LXI   D,:0008
        CALL  SUBDE     HL=start of right border
        MOV   E,C
        MVI   D,:00
        CALL  SUBDE     Subtract char offset
        CALL  CURSET    Put cursor on screen
        MVI   A,:00
        STA   ERRCOD    No extended lines
        POP   PSW       No-error return
SCS20   POP   B
        POP   D
        POP   H
        RET 
 REM
* If error 'off screen':
 REM
SCS30   POP   PSW
        MVI   A,:01     Set error code
        CMC             Change CY to 1
        JMP   SCS20     Pop, ret
*
*************************************************
* ASK CURSOR POSITION AND SIZE CHARACTER SCREEN *
*************************************************
*
* Returns the position of the cursor and the
* range of possible values.
* Values given in DE are maximum values of
* coordinates.
* If the mode is all graphics: DE=HL=0.
*
* Entry: None.
* Exit:  HL gives y,x cursor position.
*        DE gives y,x size of character part of
*           the screen (mode 0: #17,#3B: A-modes:
*           #03,#3B).
*        AFBC preserved.
*
SCURA   PUSH  PSW
        PUSH  B
        LXI   H,:0000
        MOV   D,H
        MOV   E,L       DE=HL=0
        LDA   COL1      Get current screen mode
        RAR             Char mode ?
        JNC   SCA40     Abort if not
        LHLD  LNSTR     Get startaddr cursor line
        PUSH  H         Save it on stack
        LXI   D,TICC8   Size left border
        DAD   D         Get addr 1st char byte
        XCHG            in DE
        LHLD  CURSOR    Get cursor pos addr
        XCHG
        CALL  SUBDE     Calc difference of cursor
 REM pos from begin of line
        POP   D         Get startaddr current line
        MOV   A,L       (x-coord cursor)*2 in A
        ORA   A
        RAR             Now x-coord cursor in A
        PUSH  PSW       Save it on stack
        LHLD  GAE       Get addr end char area
        LXI   B,GRR     Length 1 char line
        XRA   A         Init Y-pos
SCA10   PUSH  PSW       Save it on stack
        DAD   B         Get line mode byte next line
        CALL  COMP      Is current line this line?
        JZ    SCA20     Then jump
        POP   PSW       Get Y-coord
        INR   A         Incr it
        JMP   SCA10     Check if on next line
SCA20   POP   H         Y-coord cursor in H
        POP   PSW       X-coord cursor in A
        MOV   L,A       and now in L
        MVI   D,:17     Nr of lines for mode 0 -1
        LDA   COL1      Get current screen mode
        ORA   A
        JM    SCA30     Jump if mode 0
        MVI   D,:03     Nr of lines for A-modes -1
SCA30   MVI   E,:3B     Nr of char/line -1
SCA40   POP   B
        POP   PSW
        RET 
*
*
*
        UNL
COMP    EQU   :E6FB
XRET    EQU   :E138
COL1    EQU   :009D
SSETM   EQU   :E3D9
AESTOP  EQU   :00C6
ERRLIN  EQU   :007C
CHS     EQU   :008A
SCE     EQU   :008E
CURDEL  EQU   :E36B
HLMUL   EQU   :EB46
GAE     EQU   :008C
SSETL   EQU   :E698
SUBDE   EQU   :E6F2
CURSET  EQU   :E330
ERRCOD  EQU   :007B
LNSTR   EQU   :0078
TICC8   EQU   :FFF8
CURSOR  EQU   :0072
GRR     EQU   :0086
        LST
        END 
